<template>
    <div class="login-container">
        <div class="login-wrapper">
            <!-- 左侧品牌区域 -->
            <div class="login-left">
                <div class="brand">
                    <h1 class="brand-title">幻界之旅</h1>
                    <p class="brand-subtitle">AI chat assistant</p>
                </div>
                <div class="illustration">
                    <img src="/imgs/robot.png" alt="illustration"/>
                </div>
            </div>

            <!-- 右侧登录框 -->
            <div class="login-right">
                <div class="login-box">
                    <h2 class="login-title">欢迎登录系统</h2>
                    <form @submit.prevent="handleLogin">
                        <div class="form-group">
                            <input
                                    type="text"
                                    v-model="username"
                                    placeholder="请输入登录账号"
                                    required
                                    class="form-control"
                            />
                        </div>
                        <div class="form-group">
                            <input
                                    type="password"
                                    v-model="password"
                                    placeholder="请输入登录密码"
                                    required
                                    class="form-control"
                            />
                        </div>
                        <!-- 错误信息显示 -->
                        <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>

                        <!-- 登录按钮 -->
                        <button type="submit" class="login-button" :disabled="isLoading">
                            {{ isLoading ? '登录中...' : '登 录' }}
                        </button>

                        <!-- 注册按钮 -->
                        <router-link to="/register" class="register-button">
                            注 册
                        </router-link>
                    </form>

                    <p class="company">团队：起个好名字</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import {ref} from 'vue'
import {useRouter} from 'vue-router'
import {login} from '../services/authService'

const router = useRouter()
const username = ref('')
const password = ref('')
const errorMessage = ref('')
const isLoading = ref(false)

const handleLogin = async () => {
    isLoading.value = true
    errorMessage.value = ''

    try {
        const response = await login(username.value, password.value)
        if (response.code === 1) {
            window.location.href = '/'
        } else {
            errorMessage.value = response.msg || '登录失败，请重试'
        }
    } catch (error) {
        errorMessage.value =
            '登录失败：' + (error.response?.data?.msg || error.message)
    } finally {
        isLoading.value = false
    }
}
</script>

<style scoped>
.login-container {
    display: flex;
    min-height: 100vh;
    background: #f6f9fc;
    font-family: 'Microsoft YaHei', sans-serif;
}

.login-wrapper {
    display: flex;
    width: 1400px;
    height: 650px;
    background: #fff;
    border-radius: 28px; /* 大圆角 */
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.08);
    overflow: hidden; /* 保证子元素不会超出圆角 */
}

/* 左侧品牌区 */
.login-left {
    flex: 1.2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #eaf3ff, #f8fbff);
}

.brand {
    text-align: center;
    margin-top: 20px;
    margin-bottom: 10px;
}

.brand-logo {
    height: 50px;
    margin-bottom: 10px;
}

.brand-title {
    font-size: 22px;
    font-weight: 600;
    color: #333;
}

.brand-subtitle {
    font-size: 14px;
    color: #666;
}

.illustration img {
    max-width: 480px;
    margin-top: 50px;
    margin-bottom: 50px;
}

/* 右侧登录区 */
.login-right {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-box {
    width: 360px;
    padding: 40px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.05);
}

.login-title {
    text-align: center;
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 30px;
    color: #333;
}

.form-group {
    margin-bottom: 20px;
}

.form-control {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 15px;
    transition: all 0.3s;
}

.form-control:focus {
    border-color: #409eff;
    box-shadow: 0 0 6px rgba(64, 158, 255, 0.2);
    outline: none;
}

.form-remember {
    margin-bottom: 15px;
    font-size: 13px;
    color: #666;
}

.error-message {
    color: #e74c3c;
    font-size: 14px;
    margin-bottom: 15px;
}

.login-button {
    width: 100%;
    padding: 12px;
    background: #409eff;
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.3s;
    margin-bottom: 12px; /* 和注册按钮保持间距 */
}

.login-button:hover:not(:disabled) {
    background: #2f85f6;
}

.login-button:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.register-button {
    display: block;
    width: 100%;
    padding: 12px;
    text-align: center;
    border: 1px solid #409eff;
    border-radius: 6px;
    font-size: 16px;
    font-weight: 500;
    color: #409eff;
    background: #fff;
    transition: all 0.3s;
}

.register-button:hover {
    background: #f0f8ff;
}

.company {
    text-align: center;
    margin-top: 25px;
    font-size: 13px;
    color: #999;
}
</style>
